package org.meb.conquest.web.services;

import java.io.Serializable;

import javax.inject.Inject;
import javax.persistence.EntityManager;

import org.meb.conquest.db.model.Deck;
import org.meb.conquest.db.util.DatabaseUtils;

public class DeckServiceImpl implements DeckService, Serializable {

	private static final long serialVersionUID = -3917684297662777248L;

	@Inject
	private EntityManager em;

	@Inject
	private WebContext user;

	@Override
	public Deck saveDeck(Deck deck) {
		DatabaseUtils.executeSetUserLang(em, user.getLanguage());
		try {
			em.getTransaction().begin();
			if (deck.getId() == null) {
				em.persist(deck);
			} else {
				deck = em.merge(deck);
			}
			em.getTransaction().commit();
		} catch (RuntimeException e) {
			em.getTransaction().rollback();
			throw e;
		}
		return deck;
	}
}
